<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('代码生成列表')" />
</head>
<body>
<div class="layui-layout layui-layout-iframe">
	<div class="layui-fluid">
		<div class="layui-card">
			<!--search form-->
			<form class="layui-form layui-card-header layuiadmin-card-header-auto search-form" id="searchForm">
				<div class="layui-form-item">
					<div class="layui-inline">
						<label class="layui-form-label">表名称</label>
						<div class="layui-input-inline">
							<input class="layui-input" type="text" name="tableName" placeholder="" autocomplete="off">
						</div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">表描述</label>
						<div class="layui-input-inline">
							<input class="layui-input" type="text" name="tableComment" placeholder="" autocomplete="off">
						</div>
					</div>
					<div class="layui-inline mt5">
						<label class="layui-form-label">创建时间</label>
						<div class="layui-input-inline input-sm">
							<input class="layui-input" type="text" name="params[beginTime]" id="startTime" placeholder="开始时间" />
						</div>
						<span class="layui-form-mid">-</span>
						<div class="layui-input-inline input-sm">
							<input class="layui-input" type="text" name="params[endTime]" id="endTime" placeholder="结束时间"/>
						</div>
					</div>
					<div class="layui-inline">
						<button  type="button" class="layui-btn layuiadmin-btn-list" data-type="reload" lay-submit lay-filter="form-search" id="form-search"><i class="layui-icon">&#xe615;</i>搜索</button>
					</div>
				</div>
			</form>
			<!--table actcion-->
			<div class="layui-card-body">
				<script type="text/html" id="table_gen_toolbar">
					<div class="layui-btn-group" shiro:hasPermission="system:dict:add">
						<button class="layui-btn layui-btn-sm" lay-event="batch-gencode" shiro:hasPermission="tool:gen:code"><i class="layui-icon">&#xe654;</i>生成</button>
						<button class="layui-btn layui-btn-sm" lay-event="import"><i class="layui-icon">&#xe654;</i>导入</button>
						<button class="layui-btn layui-btn-sm" lay-event="batch-remove" shiro:hasPermission="tool:gen:remove"><i class="layui-icon">&#xe640;</i>删除</button>
					</div>
				</script>
				<table id="table_gen" lay-filter="table_gen"></table>
				<script type="text/html" id="col_operation">
					<a shiro:hasPermission="system:dict:edit"  href="javascript:;" lay-event="preview">预览</a><span class="splitor">|</span>
					<a shiro:hasPermission="system:dict:edit"  href="javascript:;" lay-event="edit">编辑</a><span class="splitor">|</span>
					<a shiro:hasPermission="system:dict:remove" href="javascript:;" lay-event="del">删除</a><span class="splitor">|</span>
					<a shiro:hasPermission="system:dict:remove" href="javascript:;" lay-event="gencode">生成代码</a>
				</script>
			</div>
		</div>
	</div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
	var prefix = ctx + "tool/gen";
</script>
<script>
	layui.use(['common'], function () {
		selectMenu(ctx + "tool/gen");//菜单选中
		var $ = layui.jquery,
				layer = layui.layer,
				form = layui.form,
				common = layui.common,
				laydate = layui.laydate,
				table = layui.table;
		//页面事件列表
		var active = {
			reload: function () {
				search(false);
			},
			remove: function (ids) {
				common.ajaxRemove(prefix + "/remove", ids, function (res) {
					active['reload'].call(this);
				});
			}
		};
		//开始日期
		var insStart = laydate.render({
			elem: '#startTime'
			, min: 0
			, done: function (value, date) {
				//更新结束日期的最小日期
				insEnd.config.min = lay.extend({}, date, {
					month: date.month - 1
				});
				//自动弹出结束日期的选择器
				insEnd.config.elem[0].focus();
			}
		});
		//结束日期
		var insEnd = laydate.render({
			elem: '#endTime'
			, min: 0
			, done: function (value, date) {
				//更新开始日期的最大日期
				insStart.config.max = lay.extend({}, date, {
					month: date.month - 1
				});
			}
		});

		//查询表单
		form.on('submit(form-search)', function (data) {
			search(true);
		});
		//表格初始化
		table.render({
			elem: '#table_gen'
			, method: 'post'
			, url: prefix + "/list" //数据接口
			, toolbar: '#table_gen_toolbar'
			, defaultToolbar:[]
			, autoSort: false 	//关闭前端排序 使用后端排序
			, limit: getPageSize()
			, limits: getPageList()
			, text: getLoadErrorMsg()
			, page: true
			, cols: new Array([
				{type: 'checkbox', width: 40}
				, {field: 'tableName', title: '表名称'}
				, {field: 'tableComment', title: '表描述'}
				, {field: 'className', title: '实体类名称'}
				, {field: 'createTime', title: '创建时间', width: 160, sort: true}
				, {field: 'updateTime', title: '更新时间', width: 160}
				, {title: '操作', align: 'center', width: 210, toolbar: '#col_operation'}
			])
		});
		//表格工具栏按钮事件
		table.on('toolbar(table_gen)', function (obj) {
			switch (obj.event) {
				case 'batch-remove':
					var checks = table.checkStatus('table_gen');
					var ids = common.joinArray(checks.data, "tableId");
					active['remove'].call(this, ids);
					break;
				case 'batch-gencode':
					var checks = table.checkStatus('table_gen');
					if(checks.data.length==0){
						layer.alert('请选择要生成的数据');
						return;
					}
					layer.confirm('确认要生成选中的' + checks.data.length + '条数据吗?', function (index) {
						var tables=common.joinArray(checks.data, "tableName");
						location.href = prefix + "/batchGenCode?tables=" + tables;
						layer.msg('执行成功,正在生成代码请稍后…', { icon: 1 });
					});
					break;
				case 'import':
					layer.open({
						type: 2,
						title: '导入表结构',
						shade: false,
						fixed: false,
						area: ['1000px', '600px'],
						content: prefix + "/importTable",
						btn: ["保存", "取消"],
						yes: function (index, layero) {
							layero.find('iframe')[0].contentWindow.submitHandler();
						}
						, cancel: function (index, layero) {
							layui.layer.close(index);
							return false;
						}
					});
					break;
			}
		});
		//表格事件
		table.on('tool(table_gen)', function (obj) {
			switch (obj.event) {
				case 'del':
					active['remove'].call(this, obj.data.tableId);
					break;
				case 'preview': //列表数据
					preview(obj.data.tableId);
					break;
				case 'gencode': //列表数据
					var tableName=obj.data.tableName;
					var genType=obj.data.genType;
					layer.confirm('确定要生成' + tableName + '表代码吗？', function (index) {
						if(genType === "0") {
							location.href = prefix + "/download/" + tableName;
							layer.msg('执行成功,正在生成代码请稍后…', { icon: 1 });
						} else if(genType === "1") {
							common.ajax.get(prefix + "/genCode/" + tableName);
						}
					});
					break;
				case 'edit':
					var url=prefix+'/edit/'+obj.data.tableId;
					parent.addTab('修改代码生成配置',url,parent.getTabId(url));
					break;
			}
		});
		//监听排序事件
		table.on('sort(table_gen)', function (obj) {
			var field = serializeJson($('#searchForm'));
			field.orderByColumn = obj.field ;
			field.isAsc = obj.type;
			table.reload('table_gen', {where: field,page:{curr: 1}});//执行重载
		});
	});

	//表格查询
	function search(isResetCurr) {
		var field = serializeJson($('#searchForm'));
		var sortParam = getSort('table_gen');
		if (sortParam) {
			field.orderByColumn = sortParam.sortField;
			field.isAsc = sortParam.sortMethod;
		}
		//执行重载
		var options = {where: field};
		if (isResetCurr == true) {
			options.page = {curr: 1};
		}
		layui.table.reload('table_gen', options);
	}

	// 预览代码
	function preview(tableId) {
		var preViewUrl = prefix + "/preview/" + tableId;
		layui.layer.load(1, {shade: [0.2, '#fff']}); //0.1透明度的白色背景});
		layui.common.ajax.get(preViewUrl, function (result) {
			layui.layer.closeAll();
			if (result.code == 0) {
				var items = [];
				$.each(result.data, function (index, value) {
					value = value.replace(/</g, "&lt;");
					value = value.replace(/>/g, "&gt;");
					var templateName = index.substring(index.lastIndexOf("/") + 1, index.length).replace(/\.vm/g, "");
					if (!layui.common.equals("sql", templateName) && !layui.common.equals("tree.html", templateName) && !layui.common.equals("sub-domain.java", templateName)) {
						items.push({
							title: templateName, content: "<pre class=\"layui-code\">" + value + "</pre>"
						})
					}
				});
				top.layer.tab({
					area: ['90%', '90%'],
					shadeClose: true,
					tab: items
				});
			} else {
				layui.layer.alert(result.msg);
			}
		});
	}
</script>
</body>
</html>